﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Intelligent.OTC.Domain.Repositories;
using Intelligent.OTC.Common.UnitOfWork;
using Intelligent.OTC.Domain.DataModel;
using System.IO;
using Intelligent.OTC.Common;
using Intelligent.OTC.Common.Utils;
using Intelligent.OTC.Domain;

namespace Intelligent.OTC.Business
{
    public class SpecialNotesService
    {
        public SpecialNotesService() {  }
        public OTCRepository CommonRep { get; set; }

        public IQueryable<SpecialNote> GetSpecialNotes(string strCustNum) { 
        
            return CommonRep.GetQueryable<SpecialNote>().Where(o => o.CustomerNum == strCustNum && o.Deal == AppContext.Current.User.Deal).AsQueryable();
        }

        public void AddOrUpdateByPara(string CustomerNum, string Legal, string Notes)
        {
            string Deal = AppContext.Current.User.Deal;
            var SpecialNote = CommonRep.GetDbSet<SpecialNote>()
                .Where(o => o.Deal == Deal && o.CustomerNum == CustomerNum && o.LegalEntity == Legal).FirstOrDefault();
            if (SpecialNote == null)
            {
                SpecialNote newSN = new SpecialNote();
                newSN.Deal = Deal;
                newSN.CustomerNum = CustomerNum;
                newSN.LegalEntity = Legal;
                newSN.SpecialNotes = Notes;
                newSN.CreateTime = AppContext.Current.User.Now;
                newSN.UpdateTime = AppContext.Current.User.Now;
                CommonRep.Add(newSN);
            }
            else {
                SpecialNote.SpecialNotes = Notes;
                SpecialNote.UpdateTime = AppContext.Current.User.Now;
            }
            CommonRep.Commit();

        }
    }
}
